// pages/addOrder/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    urlService: app.urlService,
    startDt: undefined,
    endDt: undefined,
    modalShow: false,
    keyword: '',
    name: '',
    phone: '',
    address: '',
    payAmount: 0,
    remark: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      keyword: options.keyword
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getCustom();
  },

  getCustom: function () {
    wx.showLoading();
    var that = this;
    wx.request({
      url: that.data.urlService + '/api/v1/order/GetCustomByName?keywords=' + that.data.keyword,
      method: "GET",
      dataType: "JSON",
      header: {
        'content-type': 'application/json',
        'Authorization': "Bearer " + app.token
      },
      success: function (data) {
        if (data.statusCode == 401) {
          wx.showToast({
            title: '登录超时，请重新登录',
            icon: "error"
          })
          setTimeout(function () {
            wx.navigateTo({
              url: "../login/index"
            })
          }, 1000);
          return;
        }
        wx.hideLoading()
        var json = JSON.parse(data.data);
        if (json.success) {
          that.setData({
            name: that.data.keyword,
            phone: json.data.phone,
            address: json.data.address,
            cardNo: json.data.cardNo,
            id: json.data.id
          })
        } else {
          wx.showToast({
            title: json.msg,
            icon: 'error'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  bindDateChange: function (e) {
    this.setData({
      startDt: e.detail.value
    })
  },

  bindEndDtChange: function (e) {
    this.setData({
      endDt: e.detail.value
    })
  },

  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },

  inputName: function (e) {
    this.setData({
      name: e.detail.value
    })
  },

  inputPhone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },

  inputAddress: function (e) {
    this.setData({
      address: e.detail.value
    })
  },

  inputCardNo: function (e) {
    this.setData({
      cardNo: e.detail.value
    })
  },

  inputPayAmount: function (e) {
    this.setData({
      payAmount: e.detail.value
    })
  },

  inputRemark: function (e) {
    this.setData({
      remark: e.detail.value
    })
  },

  addOrder: function () {
    wx.showLoading();
    var that = this;
    var param = {
      customId: that.data.id,
      startDt: that.data.startDt,
      endDt: that.data.endDt,
      payAmount: that.data.payAmount,
      address: that.data.address,
      remark: that.data.remark,
      name: that.data.name,
      phone: that.data.phone,
      cardNo: that.data.cardNo
    }
    if(param.name=='' || param.name==null){
      wx.showToast({
        title: '请输入客户名称',
        icon: "error"
      })
      return;
    }
    if(param.phone=='' || param.phone==null){
      wx.showToast({
        title: '请输入客户电话',
        icon: "error"
      })
      return;
    }
    if(param.address=='' || param.address==null){
      wx.showToast({
        title: '请输入客户地址',
        icon: "error"
      })
      return;
    }
    // if(param.cardNo=='' || param.cardNo==null){
    //   wx.showToast({
    //     title: '请输入身份证',
    //     icon: "error"
    //   })
    //   return;
    // }
    if(param.startDt=='' || param.startDt==null || param.endDt=='' || param.endDt==null){
      wx.showToast({
        title: '请选择租赁日期',
        icon: "error"
      })
      return;
    }
    if(param.payAmount=='' || param.payAmount==null){
      wx.showToast({
        title: '请输入支付金额',
        icon: "error"
      })
      return;
    }
    wx.request({
      url: that.data.urlService + '/api/v1/order/createLease',
      method: "POST",
      dataType: "JSON",
      data: param,
      header: {
        'content-type': 'application/json',
        'Authorization': "Bearer " + app.token
      },
      success: function (data) {
        if (data.statusCode == 401) {
          wx.showToast({
            title: '登录超时，请重新登录',
            icon: "error"
          })
          setTimeout(function () {
            wx.navigateTo({
              url: "../login/index"
            })
          }, 1000);
          return;
        }
        wx.hideLoading()
        var json = JSON.parse(data.data);
        if (json.success) {
          wx.showToast({
            title: "保存成功",
            icon: 'success'
          })
          setTimeout(function () {
            wx.switchTab({
              url: "../index/index"
            })
          }, 1000)
        } else {
          wx.showToast({
            title: json.msg,
            icon: 'error'
          })
        }
      }
    })
  }
})